﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.Repository;
using System.Web.ModelBinding;
using System.Data.Entity;

namespace RMS.Pages.Admin
{
    /* Code-Behind for Retreats web form allows inserting, updating, and deleting retreats from the database. */
     
    public partial class Retreats : System.Web.UI.Page {
        private Repository repo = new Repository();
        
        protected void Page_Load(object sender, EventArgs e) {
            Session["IsAdmin"] = true;
            
        }

        // The GetRetreatss method reads our data objects.
        public IEnumerable<Retreat> GetRetreats() {
            string selectedCategory = cbCatSelect.Value;
            if (selectedCategory.Equals("All Categories")) {
                return repo.Retreats
                .OrderBy(p => p.Ret_Title);
            }
            else {
                return repo.Retreats
                .OrderBy(p => p.Ret_Title)
                .Where((p => (p.Ret_Main_Category == selectedCategory) || (p.Ret_Category_Two == selectedCategory)
                                                                       || (p.Ret_Category_Three == selectedCategory) || (p.Ret_Category_Four == selectedCategory)));
            }
         
        }

        // Method for updating a Retreat. 
        public void UpdateRetreat(int Ret_ID) {
            
            Retreat myRetreat = repo.Retreats
            .Where(p => p.Ret_ID == Ret_ID).FirstOrDefault();
            if (myRetreat != null && TryUpdateModel(myRetreat,
            new FormValueProvider(ModelBindingExecutionContext)))
            {
                repo.SaveRetreat(myRetreat);
            }
        }

        // Method for deleting a Retreat. 
        public void DeleteRetreat(int Ret_ID)
        {
            Retreat myRetreat = repo.Retreats
            .Where(p => p.Ret_ID == Ret_ID).FirstOrDefault();
            if (myRetreat != null)
            {
                repo.DeleteRetreat(myRetreat);
            }
            
        }

        // Method for inserting a Retreat.
        public void InsertRetreat() {
            Retreat myRetreat = new Retreat();
            if (TryUpdateModel(myRetreat,
            new FormValueProvider(ModelBindingExecutionContext)))
            {
                repo.SaveRetreat(myRetreat);
            }
        }

        // Method for viewing the reservations for a Retreat
        protected void ViewReservation(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "ViewReservation")
            {
                ListViewDataItem dataItem = (ListViewDataItem)e.Item;
                int retID = (int)ListView1.DataKeys[dataItem.DisplayIndex].Value;

                Session["RetreatID"] = retID;
                Response.Redirect("/Pages/Admin/ViewReservations.aspx");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ListView1.DataBind();
        }
        
    }
}